<?php
namespace Api\Controller;
use Common\Controller\ApiController;
use Common\ORG\PublicFunction;
use Common\ORG\WxFunction;
use Think\Db;


/**

 * 用户信息获取验证

 *

 * 用户登陆和token信息获取。

 * @Author      LD(191696242@qq.com)

 * @Version     1.o

 * @Date        2020/12/15

 * @Time        16:00

 * @since

 */
class MembersController extends ApiController{

    private $category   = [
        1 => 'reg_',    //用户注册 用户短信或邮件
        2 => 'login_',  //用户登陆 用户短信或邮件
        3 => 'mpw_',    //修改密码 用户短信或邮件
        4 => 'uid_',    //token注册缓存
        5 => 'mtel_',    //修改手机号
        6 => 'mwithdraw_',    //提现验证
        7 => 'mgcb_',    //工程币支付验证
        8 => 'share_',   //分享验证
    ];  //缓存类型

    public function _initialize() {

        $getsecret  = $_SERVER['HTTP_APPSECRET'];
        //取head标签内容
        if(!preg_match('/^\w{8}-\w{4}-\w{4}-\w{4}-\w{12}$/', $getsecret) && ACTION_NAME<>'getUserInfo'){
            $this->ajaxReturn(1, 'appsecret格式错误');
        }

        if($this->appsecret <> $getsecret && ACTION_NAME<>'getUserInfo'){
            $this->ajaxReturn(1, 'appsecret错误');
        }
        parent::_initialize();
    }


    /**

     * gettoken

     * 获取用户token信息

     * 获取用户token信息，用于所有接口数据的获取验证

     * @access public

     * @param int $uid (必填：是) 用户UID

     * @param string appsecret (必填：是) 应用secret，headers提交用于token数据的生成获取

     * @since

     * @return json

     */
    public function gettoken(){

        //$appid      = M('Members')->query('select uuid_short() as token'); //mysql计算用户UUID
        $uid        = I('post.uid', 0,'intval');
        if(empty($uid)){
            $this->ajaxReturn(1,'用户ID不能为空！');
        }

        $userinfo   = M('members')->where(['uid'=>$uid])->find();
        //echo M('members')->getLastSql();exit;
        if(empty($userinfo)){
            $this->ajaxReturn(1, '用户信息不存在！');
        }

        $result = $this->generateToken($uid);
        $this->ajaxReturn(0, 'ok', ['token'=>$result]);
    }


    /**
     * sms
     * 短信获取

     * 用户登陆注册及其他场景短信获取

     * @access public

     * @param int $type (必填：是) 登陆注册等
     *
     * @param int $mobile (必填：是) 是否必填

     * @since

     * @return json

     */
    public function sms(){
        $type   = I('post.type', 0, 'intval');  //短信类型
        $mobile = I('post.mobile', '', 'trim');  //电话号码

        if(empty($this->category[$type])){
            $this->ajaxReturn(1, '发送的短信类型错误');
        }
        if (!fieldRegex($mobile,'mobile')) {
            $this->ajaxReturn(1, '手机格式错误，请重新输入');
        }

        if($type && $type <> 1) {
            $userInfo = M('members')->where(['mobile' => $mobile])->find();
            if (empty($userInfo)) {
                $this->ajaxReturn(1, '发送的手机号码不存在！');
            }
        }
        $rands  = mt_rand(100000, 999999);
        $res    = sendSms($mobile, array($rands), $this->category[$type]);

        if($res['code']==0){
            S($this->category[$type].$mobile, $rands,  900);//验证码有效期15分钟
            $this->ajaxReturn(0, 'send success');
        }

        $this->ajaxReturn(1, $res['msg']);
    }


    /**
     * email
     * 邮件验证获取

     * 用户登陆注册及其他场景短信获取

     * @access public

     * @param int $type (必填：是) 登陆注册等

     * @param int $email (必填：否) 邮件地址

     * @since

     * @return json

     */
    public function email(){
        $type   = I('post.type', 0, 'intval');  //邮件类型
        $email  = I('post.email', '', 'trim');  //验证码邮箱

        if(empty($this->category[$type])){
            $this->ajaxReturn(1, '发送的邮件类别错误');
        }
        if (!fieldRegex($email,'email')) {
            $this->ajaxReturn(1, '邮箱格式错误，请重新输入');
        }

        $userInfo   = M('members')->where(['email'=>$email])->find();
        if(empty($userInfo)){
            $this->ajaxReturn(1, '发送的邮件地址不存在！');
        }
        $rands  = mt_rand(100000, 999999);
        $res = sendMail($email, '工程桥人才网验证码', '<h1>当前验证码'.$rands.'</h1>' . date('Y-m-d H:i:s'));

        if($res){
            S($this->category[$type].$email, $rands,  900);//验证码有效期15分钟
            $this->ajaxReturn(0, 'ok');
        }

        $this->ajaxReturn(1, '邮件发送失败');

    }

    /**

     * login

     * 用户登陆

     * 获取用户登陆数据，并返回用户登陆信息

     * @access public

     * @param int       $loginype (必填：是) 登陆类型 1、微信登陆 2、短信登陆 3、密码登陆

     * @param string    $mobile (必填：是) 用户账号 可能是用户名、手机号、邮箱

     * @param string    $password (选填：是) 用户密码

     * @param int       $mobileVcode (选填：否) 手机验证码

     * @param int       $userinfo (选填：否) 微信登陆用户信息

     * @since

     * @return json

     */
    public function login(){
        if(!IS_POST){
            $this->ajaxReturn(1, '提交方式错误！');
        }
        $loginType      = I('post.login_type', 0, 'intval');
        $mobile         = I('post.mobile', '', 'trim');  //用户账号
        $password       = I('post.password', '', 'trim');   //密码
        $mobileVcode    = I('post.mobile_vcode', 0, 'intval'); //手机验证码
        $userinfo       = I('post.userinfo');//微信登陆用户信息
        $sms            = S($this->category[2].$mobile); //登陆短信验证码
        $sms            = 123456;
        $mobile_mod     = D('Members');
        if(!in_array($loginType, [1,2,3])){
            $this->ajaxReturn(1,'登陆类型错误');
        }
        if($loginType==1) {
            //微信登陆

            $wxInfo = wxUserInfo($userinfo);
            if($wxInfo['status']==1) $this->ajaxReturn(1, $wxInfo['msg']);

            $mb = M('MembersBind')->where(['openid'=>$wxInfo['data']['wxdata']['openId']])->find();
            if(!$mb){
                $this->ajaxReturn(6, '微信绑定信息不存在', $wxInfo['data']);//用户不存在
            }
            $check_map['uid'] = array('eq',$mb['uid']);
        } elseif($loginType==2) {
            //短信登陆

            if(empty($mobile) || empty($mobileVcode)){
                $this->ajaxReturn(1, '登陆信息错误');
            }

            if (!fieldRegex($mobile,'mobile')) {
                $this->ajaxReturn(1, '手机格式错误');
            }

            if(!$sms){
                $this->ajaxReturn(1, '验证码不存在或已过期');
            }

            if($sms <> $mobileVcode){
                $this->ajaxReturn(1, '手机验证码错误');
            }
            S($this->category[2].$mobile, '');
            $check_map['mobile'] = array('eq',$mobile);
        }else{
            //密码登陆

            if(empty($mobile) || empty($password)){
                $this->ajaxReturn(1, '登陆信息错误');
            }
            $account_type='username';
            if (fieldRegex($mobile,'email'))
            {
                $account_type='email';
            }elseif (fieldRegex($mobile,'mobile')) {
                $account_type='mobile';
            }
            $check_map[$account_type] = array('eq',$mobile);
        }

        if(!$user = $mobile_mod->where($check_map)->find()) {
            $this->ajaxReturn(1, L('auth_null'));//用户不存在
        }

        if(!in_array($loginType,array(1, 2)) || !empty($password)) {
            $pwd = $mobile_mod->make_md5_pwd($password, $user['pwd_hash']);
            if ($pwd <> $user['password']) {
                $this->ajaxReturn(1, L('auth_password_failed'));//密码错误
            }
        }

        if($user['status'] == 2){
            $this->ajaxReturn(1, L('account_disabled'));//停用
        }elseif($user['status'] == 0){
            $this->ajaxReturn(1, L('auth_activation'));//未激活
        }

        //登陆日志
        write_members_log($user,'login','用户登录','小程序');

        $newUser    = [
                "uid"       => $user['uid'],
                "utype"     => intval($user['utype']),
                "username"  => $user['username'],
                "email"     => $user['email'],
                "mobile"    => $user['mobile'],
                "reg_time"  => date('Y-m-d H:i:s', $user['reg_time']),
                "last_login_time"   => date('Y-m-d H:i:s', $user['reg_time']),
                "status"    => intval($user['status']), //0未激活 1正常 2 停用
                "avatars"   => !empty($user['avatars']) ? attach($user['avatars'], 'avatar') : attach('default.png', 'avatar'),
                'token'     => $result = $this->generateToken($user['uid']),
        ];

        $this->ajaxReturn(0, 'ok', $newUser);

    }

    /**

     * isUser

     * 获取用户登陆信息

     * 获取用户登陆数据，并返回用户登陆信息

     * @access public

     * @param

     * @since

     * @return json

     */
    public function isUser(){
        $mobile         = I('post.mobile', '', 'trim');  //用户账号
        $uid            = I('post.uid', 0, 'intval');    //账户UID
        $mobileVcode    = I('post.mobile_vcode', 0, 'intval'); //手机验证码
        $sms            = S($this->category[8].$mobile); //获取分享手机验证码
        $sms            = 123456;

        if (!fieldRegex($mobile,'mobile')) $this->ajaxReturn(1, '手机格式错误');

        if(empty($mobileVcode)) $this->ajaxReturn(1, '验证码不能为空');

        if(empty($sms)) $this->ajaxReturn(1, '验证码不存在或已过期');

        if($sms <> $mobileVcode) $this->ajaxReturn(1, '手机验证码错误');

        S($this->category[8].$mobile, '');

        $shareUser  = D('Members')->where(['uid'=> $uid])->find();//分享用户
        $regUser    = D('Members')->where(['mobile'=> $mobile])->find();//当前用户
        if(empty($shareUser)) $this->ajaxReturn(1, '邀请用户不存在');

        if(empty($regUser)) $this->ajaxReturn(0, '当前账户不存在');  //不存在则注册

        $this->ajaxReturn(1, '当前账户存在');  //不存在则注册
        //分享用户为人才顾问，绑定用户存在
        /*if($shareUser['utype']==3 && $regUser['utype']==2){
            $tableName  = C('DB_PREFIX').'members';
            $guwennum   = M("GuwenUser as g")->join('left join ' . $tableName . ' as m on g.uid=m.uid')->where(['g.status'=>0,'m.uid'=>$regUser['uid'], 'm.utype'=>$regUser['utype']])->count();
            if($guwennum < 3){
                M("GuwenUser")->add(['guwen_id'=>$uid, 'uid'=>$regUser['uid'], 'add_time'=>time()]);
                M("Members")->where(['uid'=>$uid])->setInc('user_count');//顾问人才数量加1
                $this->ajaxReturn(4, '邀请成功');
            }else{
                $this->ajaxReturn(5, '您的顾问名额已满，需要释放顾问名额，才能加入新顾问人才库');
            }
        }*/
    }

    /**

     * getUserInfo

     * 获取用户登陆信息

     * 获取用户登陆数据，并返回用户登陆信息

     * @access public

     * @param

     * @since

     * @return json

     */
    public function getUserInfo(){

        $mobile_mod     = D('Members');
        if(!$user = $mobile_mod->where(['uid'=>$this->userinfo['uid']])->find()) {
            $this->ajaxReturn(1, L('auth_null'));//用户不存在
        }

        $newUser    = [
            "uid"       => $user['uid'],
            "utype"     => intval($user['utype']),
            "advisor_level"         => $user['advisor_level'],
            "advisor_level_name"    => $user['advisor_level_name'],
            "username"  => $user['username'],
            "email"     => $user['email'],
            "mobile"    => $user['mobile'],
            "reg_time"  => date('Y-m-d H:i:s', $user['reg_time']),
            "last_login_time"   => date('Y-m-d H:i:s', $user['reg_time']),
            "status"    => intval($user['status']), //0未激活 1正常 2 停用 3注销
            "avatars"   => avatar($this->userinfo),
            'token'     => $_SERVER['HTTP_TOKEN'],
        ];

        $this->ajaxReturn(0, 'ok', $newUser);

    }


    /**

     * register

     * 用户注册

     * 提交用户数据数据，并返回用户注册信息

     * @access public

     * @param int       $loginype (必填：是) 登陆类型 1、微信登陆 2、短信登陆 3、密码登陆

     * @param string    $mobile (必填：是) 用户账号 可能是用户名、手机号、邮箱

     * @param string    $password (选填：是) 用户密码

     * @param int       $mobileVcode (选填：否) 手机验证码

     * @param int       $userinfo (选填：否) 微信登陆用户信息

     * @since

     * @return json

     */
    public function register() {
        if (C('qscms_closereg')) {
            $this->ajaxReturn(0, '网站暂停会员注册，请稍后再次尝试！');
        }

        if(!IS_POST){
            $this->ajaxReturn(1, '提交方式错误！');
        }
        //$utype          = I('post.utype', 0, 'intval');
        $mobile         = I('post.mobile', '', 'trim');
        $mobileVcode    = I('post.mobile_vcode', 0, 'intval');
        $email          = I('post.email', '', 'trim');
        $password       = I('post.password', '', 'trim');
        $passwordVerify = I('post.passwordVerify', '', 'trim');
        $regVerify      = S($this->category[1].$mobile); //获取缓存注册手机验证码
        $regVerify      = 123456;

        $userinfo       = I('post.userinfo');//微信登陆用户信息
        $wxUserInfo     = I('post.wx_userinfo', '', 'trim,badword');

        $uid            = I('post.uid', 0, 'intval'); //如果UID不为空，而且是顾问角色，则添加人才到人才顾问
        /*$userinfo       =[
            'encryptedData_mobile'=>"XSWcDZNQrVnqT8FV+15JUc8vBi/Dkgaw3rPKyBHozm9ID8HkLD6+OtfdDPpXUnIfMNvA3GT3ZDzJBGiJJXSHTVAEmklbe9Df0gU/UjRuAtcwAJWwIWE+D3xj0A9z+lqm/nx1AFKebSnD7eDEgIXrcrWIZ9L+H5xqeLG8+FRaupb8t/KOGMPOV8l5eS60nvHZl1xyErd2IECq+BkIt0FqjA==",
            'iv_mobile'=>"7XPfrBbg6QF+B63QTnZUxQ==",
            'code'=>"05161g000XhBRK1P7j100uBLjN161g0I",
            'encryptedData'=>"+zqyNp/k8nlq/9qKZWXInw8bs77O0nDChpllTptjz/VwOTpPVdNCq/nuSvNvIwQWdbMg1YbNZ0/oZGL5iyY5h0zgEhfUoHeZYs+sxZrN7nkmFauS4ikEXITh4zb7ub4jHdH7Qehmcy08aRZ7a8DrWxy6I+o1cqflNKzFllaZbotRfwtlpfRgUXDIj3wyaJAPRKtRKa4xPVYH499BvGjbo6poc3WRTsU+UpnFlqnodlurUceOXzlFyrtOfXB72ACad6HIsa6HWmPEj/Yq502O43EPY/pV7L3B+Ue/8afxpbMH1BIFrTMQUc+9AbLkJrA3+CK2qywMCxUM+w7FoiwVp/+3U3SrYS3chtU9ACvTFheu+EV4N6Hql1k8Xvt4RrnG/U8H8U3Ry2dA+Xm2ogXCQwXJSOYKYAVm3fN9BxqlmLBhzxDr6p3oOFSC5LJ17OVVkn5XqG+NVamo96rG/NAFNbXCG9nX8U0f5onKx43WcqQ=",
            'iv'=>"HOgbqd92G+PSfEj/Xc0GzA==",
        ];*/

        if(!empty($userinfo) || !empty($wxUserInfo)){
            if(!empty($userinfo)){
                $wxResult   = wxUserInfo($userinfo);
                if($wxResult['status']==1) $this->ajaxReturn(1, $wxResult['msg']);
                $wxInfo     = $wxResult['data'];
            }else {
                $wxInfo     = $wxUserInfo;
            }

            $mb     = M('MembersBind')->where(['type'=>'weixin', 'openid'=>$wxInfo['wxdata']['openId']])->find();
            $res    = M('Members')->where(['mobile'=>$wxInfo['mobileData']['phoneNumber']])->find();

            if (empty($res) && empty($mb)) {
                //用户信息和绑定信息都为空直接注册和绑定
                $res = $this->memberAdd($wxInfo['mobileData']['phoneNumber'], '123456', $email);
                //绑定微信账号
                $bind_user = array(
                    'uid'   => $res['uid'],
                    'type'  => 'weixin',
                    'keyid' => '',
                    'info'  => json_encode($wxInfo),
                    'bindingtime' => time(),
                    'openid'    => $wxInfo['wxdata']['openId'],
                    'unionid'   => $wxInfo['wxdata']['unionid'] ?: '',
                    'is_bind'   => 1
                );
                M('MembersBind')->add($bind_user);
            } elseif (!empty($res) && empty($mb)) {
                //手机注册信息存在，绑定信息不存在直接绑定
                $bind_user = array(
                    'uid'   => $res['uid'],
                    'type'  => 'weixin',
                    'keyid' => '',
                    'info'  => json_encode($wxInfo),
                    'bindingtime' => time(),
                    'openid'    => $wxInfo['wxdata']['openId'],
                    'unionid'   => $wxInfo['wxdata']['unionid'] ?: '',
                    'is_bind'   => 1
                );
                M('MembersBind')->add($bind_user);
            }else {
                if ($mb['uid'] <> $res['uid']) {
                    $this->ajaxReturn(1, '绑定信息和用户信息不一致');
                }
            }

        }else {
            //手机号验证码注册
            if(!$regVerify){
                $this->ajaxReturn(1, '验证码已过期！');
            }
            if($regVerify <> $mobileVcode){
                $this->ajaxReturn(1, '验证码错误！');
            }else{
                S($this->category[1].$mobile, '');
            }

            if (empty($mobile) || !fieldRegex($mobile,'mobile')) {
                $this->ajaxReturn(1, '手机号码为空或格式错误！');
            }

            if (empty($email) || !fieldRegex($email,'email'))
            {
                $this->ajaxReturn(1, '邮件为空或格式错误！');
            }

            if (empty($password) || empty($passwordVerify)) {
                $this->ajaxReturn(1, '密码或重复密码不能为空！');
            }

            if(strlen($password) < 6){
                $this->ajaxReturn(1, '密码不能低于6位数！');
            }

            if ($password <> $passwordVerify) {
                $this->ajaxReturn(1, '两次密码输入不一致！');
            }

            $isuser = M('Members')->where(['mobile'=>$mobile])->find();
            $isemail= M('Members')->where(['email'=>$email])->find();

            if ($isuser) {
                $this->ajaxReturn(1, '该手机号用户已经存在');
            }elseif ($isemail){
                $this->ajaxReturn(1, '邮件地址已经存在');
            }else{
                //注册用户
                $res = $this->memberAdd($mobile, $password, $email);
            }
        }
        $newUser    = [
            "uid"       => $res['uid'],
            "utype"     => intval($res['utype']),
            "username"  => $res['username'],
            "email"     => $res['email'],
            "mobile"    => $res['mobile'],
            "reg_time"  => date('Y-m-d H:i:s', $res['reg_time']),
            "last_login_time"   => date('Y-m-d H:i:s', $res['reg_time']),
            "status"    => intval($res['status']), //0未激活 1正常 2 停用
            "avatars"   => $res['avatars'],
            'token'     => $result = $this->generateToken($res['uid']),
        ];

        //判断是否有上级UID，如果存在并且还是人才顾问，则添加当前注册账户到人才库
        if($uid){
            $members    = D("Members")->get_user_one(['uid'=>$uid]);
            if($members && $members['utype']==3){
                M("GuwenUser")->add(['guwen_id'=>$uid, 'uid'=>$res['uid'], 'add_time'=>time()]);
                M("Members")->where(['uid'=>$uid])->setInc('user_count');//顾问人才数量加1
            }
        }

        $this->ajaxReturn(0, 'ok', $newUser);

    }


    /**

     * modifyUser

     * 修改用户类型

     * 修改用户类型

     * @access public

     * @param int       $uid (必填：是) 用户UID

     * @param string    $mobile (必填：是) 用户账号 可能是用户名、手机号、邮箱

     * @param string    $password (选填：是) 用户密码

     * @param int       $mobileVcode (选填：否) 手机验证码

     * @param int       $userinfo (选填：否) 微信登陆用户信息

     * @since

     * @return json

     */
    public function modifyUser(){
        $uid    = I('post.uid', 0, 'intval');
        $utype  = I('post.utype', 0, 'intval');
        if(!$uid){
            $this->ajaxReturn(1, '用户参数不能为空');
        }

        if(!$utype || !in_array($utype, array(2,3))){
            $this->ajaxReturn(1, '用户类型为空或超出范围');
        }

        $user = M('Members')->where(['uid'=>$uid])->find();
        if(!$user){
            $this->ajaxReturn(1, '修改的用户不存在');
        }

        if($user['utype']==$utype){
            $newUser    = [
                "uid"       => $user['uid'],
                "utype"     => intval($utype),
                "username"  => $user['username'],
                "email"     => $user['email'],
                "mobile"    => $user['mobile'],
                "reg_time"  => date('Y-m-d H:i:s', $user['reg_time']),
                "last_login_time"   => date('Y-m-d H:i:s', $user['reg_time']),
                "status"    => intval($user['status']), //0未激活 1正常 2 停用
                "avatars"   => !empty($user['avatars']) ? attach($user['avatars'], 'avatar') : attach('default.png', 'avatar'),
            ];
            $this->ajaxReturn(0, 'ok', $newUser);
        }

        $res = M('Members')->where(['uid'=>$user['uid']])->save(['utype'=> $utype]);
        if($res){

            //修改用户搜索类型
            if(M('ResumeSearchFull')->where(['uid'=> $user['uid']])->find()) {
                M('ResumeSearchFull')->where(['uid' => $user['uid']])->save(['utype' => $utype]);
            }else{
                //M('ResumeSearchFull')->add(['uid' => $user['uid'], 'utype' => $utype]);
            }

            if(M('ResumeSearchPrecise')->where(['uid'=> $user['uid']])->find()) {
                M('ResumeSearchPrecise')->where(['uid' => $user['uid']])->save(['utype' => $utype]);
            }else{
                //M('ResumeSearchPrecise')->add(['uid' => $user['uid'], 'utype' => $utype]);
            }

            //成为人才顾问删除相关企业资料
            M('CompanyProfile')->where(['uid'=>$user['uid']])->delete();
            M('companyImg')->where(['uid'=>$user['uid']])->delete();//图片

            if($utype==2){
                M('QuestionnaireUserList')->where(['uid'=>$user['uid']])->delete();//修改求职者，删除问答
            }



            $newUser    = [
                "uid"       => $user['uid'],
                "utype"     => intval($utype),
                "username"  => $user['username'],
                "email"     => $user['email'],
                "mobile"    => $user['mobile'],
                "reg_time"  => date('Y-m-d H:i:s', $user['reg_time']),
                "last_login_time"   => date('Y-m-d H:i:s', $user['reg_time']),
                "status"    => intval($user['status']), //0未激活 1正常 2 停用
                "avatars"   => !empty($user['avatars']) ? attach($user['avatars'], 'avatar') : attach('default.png', 'avatar'),
            ];
            $this->ajaxReturn(0, '用户类型更改成功', $newUser);
        }else {
            $this->ajaxReturn(1, '用户类型更改失败');
        }

    }


    /**

     * modifyPassword

     * 密码修改

     * 重置用户密码

     * @access public

     * @param string    $mobile (必填：是) 用户账号 可能说手机号、邮箱

     * @param int       $mobileVcode (选填：是) 短信验证码

     * @param string    $password (选填：是) 用户密码

     * @param string    $passwordVerify (选填：是) 重复密码


     * @since

     * @return json

     */
    public function modifyPassword(){
        $mobile         = I('post.mobile', '', 'trim');
        $mobileVcode    = I('post.mobile_vcode', 0, 'intval');
        $password       = I('post.password', '', 'trim');
        $passwordVerify = I('post.passwordVerify', '', 'trim');
        $mobile_mod     = D('Members');
        $sms            = S($this->category[3].$mobile); //短信验证码
        if(!$mobile){
            $this->ajaxReturn(1, '手机号码或邮箱不能为空');
        }

        if(!$mobileVcode){
            $this->ajaxReturn(1, '验证码不能为空');
        }

        if (empty($password) || empty($passwordVerify)) {
            $this->ajaxReturn(1, '密码或重复密码不能为空！');
        }

        if ($password <> $passwordVerify) {
            $this->ajaxReturn(1, '两次密码输入不一致！');
        }

        if (!fieldRegex($mobile,'mobile') && !fieldRegex($mobile,'email')) {
            $this->ajaxReturn(1, '手机或邮箱格式错误！');
        }

        if($sms <> $mobileVcode){
            $this->ajaxReturn(1, '验证码错误！');
        }else{
            S($this->category[3].$mobile, '');
        }


        if (fieldRegex($mobile,'email'))
        {
            $account_type='email';
        }elseif (fieldRegex($mobile,'mobile')) {
            $account_type='mobile';
        }

        $user = M('Members')->where([$account_type=>$mobile])->find();
        if(!$user){
            $this->ajaxReturn(1, '修改的用户不存在');
        }

        $pwd = $mobile_mod->make_md5_pwd($password, $user['pwd_hash']);
        if($pwd==$user['password']){
            $this->ajaxReturn(0, '密码重置成功');
        }
        $res = $mobile_mod->where(['uid'=>$user['uid']])->save(['password'=> $pwd]);
        if($res){
            $this->ajaxReturn(0, '密码重置成功');
        }else {
            $this->ajaxReturn(1, '密码重置失败');
        }

    }



    /**

     * agreement

     * 服务条款

     * 工程桥注册服务条款

     * @access public

     * @since

     * @return json

     */
    public function agreement(){
        $agreement = M('Text')->where(array('name'=>'agreement'))->find();

        $this->ajaxReturn(0, 'ok', ['text'=>!empty($agreement['value']) ? $agreement['value']:'']);

    }


    /**

     * conceal

     * 隐私协议

     * 工程桥注册隐私协议

     * @access public

     * @since

     * @return json

     */
    public function conceal(){
        $agreement = M('Text')->where(array('name'=>'conceal'))->find();

        $this->ajaxReturn(0, 'ok', ['text'=>!empty($agreement['value']) ? $agreement['value']:'']);
    }

    /**

     * about
     * 关于我们

     * 工程桥关于我们说明

     * @access public

     * @since

     * @return json

     */
    public function about(){
        $agreement = M('Text')->where(array('name'=>'about'))->find();

        $this->ajaxReturn(0, 'ok', ['text'=>!empty($agreement['value']) ? $agreement['value']:'']);
    }

    /**

     * help
     * 帮助说明

     * 工程桥帮助说明

     * @access public

     * @since

     * @return json

     */
    public function help(){
        $agreement = M('Text')->where(array('name'=>'help'))->find();

        $this->ajaxReturn(0, 'ok', ['text'=>!empty($agreement['value']) ? $agreement['value']:'']);
    }

    //注册members表
    private function memberAdd($mobile, $password, $email){
        $data   = array(
            'utype'     => 0,
            'mobile'    => $mobile,
            'email'     => $email,
            'password'  => $password
        );
        $data   = $this->newName($data);    //添加用户名
        if(false === $res = $this->_local_add($data)){
            $this->ajaxReturn(1, '用户注册失败');
        }

        return $res;
    }

    //生成token
    private function generateToken($uid){

        $randStr    = PublicFunction::createNoncestr(6);//随机字符串
        $data       = array(
            'uid'       => $uid,
            'appkey'    => $this->appkey,
            'appsecret' => $this->appsecret,
            'randStr'   => $randStr
        );
        $options    = 0;//数据格式选项（可选）
        $iv         = '';//加密初始化向量（可选）
        $result = openssl_encrypt(json_encode($data), 'AES-128-CBC', $this->appsecret, $options, $iv); //AES-128-CBC加密方式

        S($this->category[4].$uid, $result, 86400*2);
        return $result;
    }

}
?>